package cn.afactory.tygz.ui.base.views.subject;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsSubjectDetail;
import cn.afactory.tygz.ui.base.adapter.SubjectImgGirdAdapter;
import cn.afactory.tygz.ui.base.utils.sns.TextUtilListener;
import cn.afactory.tygz.ui.base.utils.sns.ViewTools;
import cn.afactory.tygz.ui.base.views.AutoGridView;

public class SubjectDetailHeaderView extends LinearLayout{

    private AutoGridView mGridView;

    private OnItemClickListener mItemClickListener;
    private SubjectImgGirdAdapter bm;
    private View mView;
    private int mIamgeWidth = 0, mIamgeHeight = 0;

    public SnsSubjectDetail mItem = null;
    //是否显示健康圈的跳转广告条
    public boolean isShow = false;
    private Context mContext;


    public SubjectDetailHeaderView(Context context, boolean show) {
        super(context);
        initView();
        isShow = show;
    }
    public SubjectDetailHeaderView(Context context, AttributeSet attrs, boolean show) {
        this(context, attrs, 0,show);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SubjectDetailHeaderView(Context context, AttributeSet attrs, int defStyle, boolean show) {
        super(context, attrs, defStyle);
        initView();
        isShow = show;
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        mView = inflater.inflate(R.layout.health_circle_detail_headerview, this, true);
//        findview(mView);

        mContext = getContext();

        mIamgeWidth = (ViewTools.getScreenWidth(getContext()) - getContext().getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding15) * 2
                - getContext().getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2) * 2) / 3;
        mIamgeHeight = mIamgeWidth + getContext().getResources().getDimensionPixelOffset(R.dimen.healthcircle_padding2);
    }


    public interface OnItemClickListener extends TextUtilListener {
        public void onPraiseSubject(TextView v, SnsSubjectDetail item);
        public void onDeletesSubject(long subjectId);
        public void onClickImage(int position, List<String> mPath);

        public void onTitleView();
        //资讯VIew
        void onClickExt(String extContentType, String extUrl);
        //来自于
        void onClickSource(String source);
        //头像点击
        void onClickPortrait(long userId);
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    public void setData(final SnsSubjectDetail item){
        mItem = item;
        if(item == null ) {
            return;
        }

        final ViewHolder viewHolder = getViewHolder(mView);
//        if (item.subjectInfo != null && item.subjectInfo.textContent != null && !TextUtils.isEmpty(item.subjectInfo.textContent)) {
//            viewHolder.tvShortDes.setVisibility(View.VISIBLE);
//            viewHolder.tvShortDes.setText(item.subjectInfo.textContent);
//            viewHolder.tvShortDes.setOnLongClickListener(new OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View view) {
//                    showCopyDialog(view,item.subjectInfo.textContent);
//                    return false;
//                }
//            });
//        } else {
//            viewHolder.tvShortDes.setVisibility(View.GONE);
//            viewHolder.tvShortDes.setText("");
//        }
//
//        HealthCircleTextUtil.SetLinkClickIntercept(mContext, viewHolder.tvShortDes, mItemClickListener);
//
//        if(isShow){
//            viewHolder.rlhome.setVisibility(View.VISIBLE);
//            viewHolder.rlhome.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mItemClickListener.onTitleView();
////                    ExecuteSchemeUtil.gotoHealthCircle(.getContext());
//                }
//            });
//        } else {
//            viewHolder.rlhome.setVisibility(View.GONE);
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.gmtCreated > 0) {
//            viewHolder.personTime.setText(TimeUtil.getFormatDateTimeStrIgnoreFuture(item.subjectInfo.gmtCreated, getContext()));
//        } else {
//            viewHolder.personTime.setText("");
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.nick != null) {
//            viewHolder.personName.setText(item.subjectInfo.userInfo.nick);
//        } else {
//            viewHolder.personName.setText("");
//        }
//
////        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
////                && item.subjectInfo.userInfo.avatar != null) {
////            BaseImgView.loadimg(viewHolder.personPortrait, item.subjectInfo.userInfo.avatar, R.drawable.ground_touxiang,
////                    R.drawable.ground_touxiang, R.drawable.ground_touxiang, ImageScaleType.EXACTLY,
////                    (int) getContext().getResources().getDimension(R.dimen.healthcircle_padding30),
////                    (int) getContext().getResources().getDimension(R.dimen.healthcircle_padding30), 0);
////        } else {
////            viewHolder.personPortrait.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ground_touxiang));
////        }
//
//        if(item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.userId > 0){
//            viewHolder.personPortrait.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mItemClickListener.onClickPortrait(item.subjectInfo.userInfo.userId);
//                }
//            });
//        }
//
////        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
////                && item.subjectInfo.userInfo.gender != null) {
////            viewHolder.ivSex.setVisibility(View.VISIBLE);
////            if (item.subjectInfo.userInfo.gender.equals(Gendar.GENDAR_FEMALE)) {
////                viewHolder.ivSex.setImageDrawable(getContext().getResources().getDrawable(R.drawable.tag_female));
////            } else if (item.subjectInfo.userInfo.gender.equals(Gendar.GENDAR_MALE)) {
////                viewHolder.ivSex.setImageDrawable(getContext().getResources().getDrawable(R.drawable.tag_male));
////            } else {
////                viewHolder.ivSex.setVisibility(View.GONE);
////            }
////        } else {
////            viewHolder.ivSex.setVisibility(View.GONE);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.pics != null
//                && item.subjectInfo.pics.size() > 0) {
//            viewHolder.gvSubject.setVisibility(View.VISIBLE);
//            bm = new SubjectImgGirdAdapter(getContext(), item.subjectInfo.pics, mIamgeWidth, mIamgeHeight);
//            viewHolder.gvSubject.setAdapter(bm);
//            bm.setListener(new ViewListener(){
//                @Override
//                public void onClick(int position, List<String> mPath) {
//                    mItemClickListener.onClickImage(position, mPath);
//                }
//            });
//
////            viewHolder.gvSubject.setFocusable(false);
////            viewHolder.gvSubject.setEnabled(false);
////            viewHolder.gvSubject.setPressed(false);
////            viewHolder.gvSubject.setClickable(false);
//        } else {
//            viewHolder.gvSubject.setVisibility(View.GONE);
//        }
//
////        if (item.subjectInfo != null && item.subjectInfo.poiInfo != null
////                && item.subjectInfo.poiInfo.detail != null) {
////            viewHolder.tvShortDes.setVisibility(View.VISIBLE);
////            viewHolder.tvLoc.setText(item.subjectInfo.poiInfo.detail);
////            viewHolder.tvLoc.setGravity(Gravity.LEFT|Gravity.CENTER);
////        } else {
//////            viewHolder.tvLoc.setText("");
////            viewHolder.tvLoc.setVisibility(View.GONE);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.praised) {
//            ViewTools.setPraiseView(viewHolder.tvLike, true, item.praiseNum);
//        } else {
//            ViewTools.setPraiseView(viewHolder.tvLike, false, item.praiseNum);
//        }
//        viewHolder.tvLike.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(final View v) {
//                try {
//                    mItemClickListener.onPraiseSubject((TextView) v, item);
//                    ViewTools.setPraiseAni((TextView) v);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//
////        if (item.commentNum > 0) {
////            viewHolder.tvChat.setText(String.valueOf(item.commentNum));
////            ViewTools.setTextLeftIcon(viewHolder.tvChat, R.drawable.tag_comment_on);
////        } else {
////            viewHolder.tvChat.setText("评论");
////            ViewTools.setTextLeftIcon(viewHolder.tvChat, R.drawable.tag_comment);
////        }
//
//        if (item.subjectInfo != null && item.subjectInfo.isHot) {
//            viewHolder.ivTag.setVisibility(View.VISIBLE);
//        } else {
//            viewHolder.ivTag.setVisibility(View.GONE);
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.owner) {
//            viewHolder.tvDel.setVisibility(View.VISIBLE);
//            viewHolder.tvDel.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mItemClickListener.onDeletesSubject(item.subjectInfo.id);
////                    HealthCircleControler.showDeleteSubjectDialog(getContext(),item.subjectInfo.id);
//                }
//            });
//        } else {
//            viewHolder.tvDel.setVisibility(View.GONE);
//        }
//
//        if(item.subjectInfo != null && !TextUtils.isEmpty(item.subjectInfo.extContent)){
//            viewHolder.tvExtContent.setText(item.subjectInfo.extContent);
//        }else {
//            viewHolder.tvExtContent.setText("");
//        }
////        if(item.subjectInfo != null && !TextUtils.isEmpty(item.subjectInfo.extPic)){
////            BaseImgView.loadimg(viewHolder.ivExtContent, ImageUtils.getThumbnailFullPath(item.subjectInfo.extPic,
////                            (int) mContext.getResources().getDimension(R.dimen.margin_45dp) + "x" +
////                                    (int) mContext.getResources().getDimension(R.dimen.margin_45dp)), R.drawable.ground_liebiaomoren,
////                    R.drawable.ground_liebiaomoren, R.drawable.ground_liebiaomoren, ImageScaleType.EXACTLY,
////                    (int) mContext.getResources().getDimension(R.dimen.margin_45dp),
////                    (int) mContext.getResources().getDimension(R.dimen.margin_45dp), -1);
////        } else {
////            viewHolder.ivExtContent.setImageResource(R.drawable.app_icon);
////        }
//
//        if(item.subjectInfo != null && !TextUtils.isEmpty(item.subjectInfo.extContentType)){
//            viewHolder.llContent.setVisibility(View.VISIBLE);
//            viewHolder.llContent.setOnClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    mItemClickListener.onClickExt(item.subjectInfo.extContentType, item.subjectInfo.extUrl);
//                }
//            });
//        } else {
//            viewHolder.llContent.setVisibility(View.GONE);
//        }
//
//        if(item.subjectInfo != null && !TextUtils.isEmpty(item.subjectInfo.sourceDesc)){
//            viewHolder.tvSource.setText(item.subjectInfo.sourceDesc);
//            viewHolder.llSourceTitle.setVisibility(View.VISIBLE);
//            if(!TextUtils.isEmpty(item.subjectInfo.source)){
//                viewHolder.tvSource.setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mItemClickListener.onClickSource(item.subjectInfo.source);
//                    }
//                });
//            } else {
//                viewHolder.tvSource.setOnClickListener(null);
//            }
//        } else {
//            viewHolder.llSourceTitle.setVisibility(View.GONE);
//        }
//
//        if (item.subjectInfo != null && item.subjectInfo.userInfo != null
//                && item.subjectInfo.userInfo.level != null) {
//            if("VERIFIED".equals(item.subjectInfo.userInfo.level)){
//                viewHolder.ivVip.setVisibility(View.VISIBLE);
//            }else {
//                viewHolder.ivVip.setVisibility(View.GONE);
//            }
//        }

//        ViewGroup.LayoutParams la = viewHolder.rlPraise.getLayoutParams();
//        la.height = getContext().getResources().getDimensionPixelOffset(R.dimen.padding_30dp) + BitmapHelp.getFontHeight(viewHolder.tvLike.getPaint());
//        viewHolder.rlPraise.setLayoutParams(la);

    }

    private void showCopyDialog(View v,final String mMsg){
//        final Context ctx = v.getContext();
//        String[] operations = ctx.getResources().getStringArray(com.pingan.im.ui.R.array.im_ask_ops);
//        new AlertDialog.Builder(ctx).setItems(operations,
//                new DialogInterface.OnClickListener() {
//                    public void onClick(DialogInterface dialog, int which) {
//                        switch (which) {
//                            case 0:// 复制消息内容
//                                ClipboardManager manager = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
//                                manager.setText(mMsg);
//                                break;
//                            default:
//                                break;
//                        }
//                    }
//                }
//        ).show();
    }

    private ViewHolder getViewHolder(View view){
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.personPortrait = (ImageView) view.findViewById(R.id.subject_submitter_pic);
        viewHolder.personTime = (TextView) view.findViewById(R.id.subject_submitter_time);
        viewHolder.personName = (TextView) view.findViewById(R.id.subject_submitter_name);
        viewHolder.tvShortDes = (TextView) view.findViewById(R.id.subject_shordes);
        viewHolder.ivSex = (ImageView) view.findViewById(R.id.subject_sex);
        viewHolder.tvLoc = (TextView) view.findViewById(R.id.tv_loc);
        viewHolder.tvLike = (TextView) view.findViewById(R.id.tv_like);
        viewHolder.tvChat = (TextView) view.findViewById(R.id.tv_chat);
        viewHolder.ivTag = (ImageView) view.findViewById(R.id.iv_tag);
        viewHolder.tvDel = (TextView) view.findViewById(R.id.subject_delete);
        viewHolder.gvSubject = (AutoGridView) view.findViewById(R.id.subject_gridview);
        viewHolder.rlPraise = (LinearLayout) view.findViewById(R.id.subject_comment_praise);
        viewHolder.rlhome = (RelativeLayout) view.findViewById(R.id.subject_back_home);
        viewHolder.ivExtContent = (ImageView) view.findViewById(R.id.iv_extContent);
        viewHolder.tvExtContent = (TextView) view.findViewById(R.id.tv_extContent);
        viewHolder.llContent = (LinearLayout) view.findViewById(R.id.ll_content);
        viewHolder.tvSource = (TextView) view.findViewById(R.id.tv_source);
        viewHolder.llSourceTitle = (LinearLayout) view.findViewById(R.id.ll_source);
        viewHolder.ivVip = (ImageView) view.findViewById(R.id.subject_vip);
        viewHolder.tvShortDes.setTextIsSelectable(true);
        return viewHolder;
    }

    private static class ViewHolder {
        TextView personTime;
        ImageView personPortrait;
        TextView personName;
        TextView tvShortDes;
        ImageView ivSex;
        ImageView ivTag;
        TextView tvDel;
        TextView tvLoc;
        TextView tvLike;
        TextView tvChat;
        AutoGridView gvSubject;
        LinearLayout rlPraise;
        RelativeLayout rlhome;
        ImageView ivExtContent;
        TextView tvExtContent;
        LinearLayout llContent;
        TextView tvSource;
        LinearLayout llSourceTitle;
        ImageView ivVip;
    }

}
